<template>
 <view>
   <view class="my-search-box">
     <my-search @click.native="gotoSearch"></my-search>
   </view>
   
    <view class="scroll-view-container">
      <!-- 左侧的滚动视图区域 -->
      <scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
        <block v-for="(item,i) in cateList" :key="i">
          <view class="left-scroll-view-item" :class="{active:i==active}" @click="activeChange(i)">{{item.cat_name}}</view>
        </block>
      </scroll-view>
      <!-- 右侧的滚动视图区域 -->
      <scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop">
        <view class="catelv2" v-for="(item,i) in cateLevel2" :key="i">
          <view class="catelv2-title">
            /{{item.cat_name}}/
          </view>
            <view class="catelv3">
              <view class="leve3item" v-for="(item1,i) in item.children" :key="i" @click="getGoodsList(item1)">
                <image :src="item1.cat_icon" mode="widthFix"></image>
                <text class="catName">{{item1.cat_name}}</text>
              </view>
            </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
// 导入自己封装的 mixin 模块
import badgeMix from '@/mixins/tabbar-badge.js'

export default {
  // 将 badgeMix 混入到当前的页面中进行使用
  mixins: [badgeMix],
    data() {
      return {
        wh:0,
        cateList:[],
        active:0,
        scrollTop:0
      };
    },
    computed:{
      cateLevel2(){
        const cateListValue=this.cateList[this.active]
        return cateListValue?cateListValue.children:[]
      },
    },
    onLoad(){
      const sysInfo=uni.getSystemInfoSync()
      this.wh=sysInfo.windowHeight-50
      this.getCateList()
    },
    methods:{
      //去搜索页
      gotoSearch(){
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      },
      async getCateList(){
        const {data:res} = await uni.$http.get('categories')
        console.log(res);
        if(res.meta.status!==200) return uni.$showMsg
        this.cateList=res.message
      },
      //分类列表切换一级标题
      activeChange(i){
        this.active=i
        this.scrollTop=this.scrollTop===0?1:0
      },
      //点击分类三级列表展示商品详情
      getGoodsList(item1){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid=' + item1.cat_id
        })
      }
    }
  }
</script>

<style lang="scss">
  .my-search-box{
    position: sticky;
    top: 0;
    z-index: 999;
  }
.scroll-view-container{
 
  display: flex;
  .left-scroll-view{
    width: 120px;
    .left-scroll-view-item{
      line-height: 60px;
      text-align: center;
     background-color: #f7f7f7;
      &.active{
        background-color: #ffffff;
        position: relative;
        &::before{
          content: '';
          display: block;
          width: 3px;
          height: 30px;
          background-color: red;
          position: absolute;
          top: 50%;
          left: 0;
          transform: translateY(-50%);
        }
      }
  }
  }
.right-scroll-view{
  background-color: #ffffff;
  .catelv2{
      margin-top: 20px;
    .catelv2-title{
      height: 30px;
      line-height: 30px;
      font-size: 12px;
      text-align: center;
    }
    .catelv3{
      display: flex;
      flex-wrap: wrap;
       
      
      .leve3item{
        display: flex;
        flex-direction: column;
        width: 33.3%;
        align-items: center;
        image{
           width: 60px;
        }
       
        
        .catName{
          font-size: 12px;
          text-align: center;
        }
      }
    }
  }
}
}
</style>
